
timepoints=[-48 -24 6 18 24 48 72];

mean_bxc=[];
for region_ind=1:17
    region_ind
    mean_bxc(:,region_ind)=NaN;
    for t_ind=1:size(hom_data(region_ind).raw_data,2)
        bin_act=[];
        bxc=[];
        l_ind=hom_data(region_ind).raw_data(t_ind).act_code>0;
        for cell_ind=1:size(hom_data(region_ind).raw_data(t_ind).sig_act,2)
            
            tmp=hom_data(region_ind).raw_data(t_ind).sig_act(l_ind,cell_ind);
            tmp=tmp./repmat(median(tmp),size(tmp,1),1);
            
            bin_act(:,cell_ind)=tmp>1.5;
        end
        
            bin_act=bin_act(:,hom_data(region_ind).raw_data(t_ind).ROItype==99);
       
        cnt=0;
        for ind=1:size(bin_act,2)
            for knd=ind+1:size(bin_act,2);
                cnt=cnt+1;
                bxc(cnt)=bin_act(:,ind)'*bin_act(:,knd)./norm(bin_act(:,ind))./norm(bin_act(:,knd));
            end
        end
        mean_bxc(find(hom_data(region_ind).raw_data(t_ind).timepoint==timepoints),region_ind)=nanmean(bxc);
    end
end

mean_bxc2=[];
mean_cnt=0;
for region_ind=18:31
    region_ind
    mean_cnt=mean_cnt+1;
    mean_bxc2(:,mean_cnt)=NaN;
    for t_ind=1:size(hom_data(region_ind).raw_data,2)
        bin_act=[];
        bxc=[];
        l_ind=hom_data(region_ind).raw_data(t_ind).act_code>0;
        for cell_ind=1:size(hom_data(region_ind).raw_data(t_ind).sig_act,2)
            
            tmp=hom_data(region_ind).raw_data(t_ind).sig_act(l_ind,cell_ind);
            tmp=tmp./repmat(median(tmp),size(tmp,1),1);
            
            bin_act(:,cell_ind)=tmp>1.5;
        end
        
        bin_act=bin_act(:,hom_data(region_ind).raw_data(t_ind).ROItype==99);
        
        cnt=0;
        for ind=1:size(bin_act,2)
            for knd=ind+1:size(bin_act,2);
                cnt=cnt+1;
                bxc(cnt)=bin_act(:,ind)'*bin_act(:,knd)./norm(bin_act(:,ind))./norm(bin_act(:,knd));
            end
        end
        mean_bxc2(find(hom_data(region_ind).raw_data(t_ind).timepoint==timepoints),mean_cnt)=nanmean(bxc);
    end
end
% % % 
% % % mean_bxc(:,4)=[]
% % % mean_bxc2(:,14)=[]
% % % mean_bxc2(:,2)=[]

% 
% mean_bxc(3,4)=NaN
% mean_bxc2(1,14)=NaN
% mean_bxc2(6,2)=NaN

n=2.5; %line width

%mean_bxc=[mean_bxc_old mean_bxc_new(1:6,:)]

nc=nanmean(nanmean(mean_bxc(1:2,:)'));
nc2=nanmean(nanmean(mean_bxc2(1:2,:)'));

figure;hold on
plot(timepoints(1:2),nanmean(mean_bxc(1:2,:)')/nc,'ko','linewidth',n)
plot(timepoints(3:6),nanmean(mean_bxc(3:6,:)')/nc,'ko','linewidth',n)
errorbar(timepoints(1:2),nanmean(mean_bxc(1:2,:)')/nc,(nanstd(mean_bxc(1:2,:)')/sqrt(size(mean_bxc(1:2,:),2)))/nc, 'k', 'LineWidth', n);
errorbar(timepoints(3:6),nanmean(mean_bxc(3:6,:)')/nc,(nanstd(mean_bxc(3:6,:)')/sqrt(size(mean_bxc(3:6,:),2)))/nc, 'k', 'LineWidth', n);

plot(timepoints(1:2),nanmean(mean_bxc2(1:2,:)')/nc2,'ro','linewidth',n)
plot(timepoints(3:6),nanmean(mean_bxc2(3:6,:)')/nc2,'ro','linewidth',n)
errorbar(timepoints(1:2),nanmean(mean_bxc2(1:2,:)')/nc2,(nanstd(mean_bxc2(1:2,:)')/sqrt(size(mean_bxc2(1:2,:),2)))/nc2, 'r', 'LineWidth', n);
errorbar(timepoints(3:6),nanmean(mean_bxc2(3:6,:)')/nc2,(nanstd(mean_bxc2(3:6,:)')/sqrt(size(mean_bxc2(3:6,:),2)))/nc2, 'r', 'LineWidth', n);

ylabel('average cross correlation')
xlabel('timepoint')


set(gca,'xtick',timepoints)
















mean_bxc_new=[];
for region_ind=1:24
    region_ind
    mean_bxc_new(:,region_ind)=NaN;
    for t_ind=1:size(hom_data2(region_ind).raw_data,2)
        bin_act=[];
        bxc=[];
        l_ind=hom_data2(region_ind).raw_data(t_ind).act_code>0;
        
        l_ind=l_ind(1:4:end-1);

        for cell_ind=1:size(hom_data2(region_ind).raw_data(t_ind).sig_act,2)
            
            tmp=hom_data2(region_ind).raw_data(t_ind).sig_act(l_ind,cell_ind);
            tmp=tmp./repmat(median(tmp),size(tmp,1),1);
            
            bin_act(:,cell_ind)=tmp>1.5;
        end
       
        cnt=0;
        for ind=1:size(bin_act,2)
            for knd=ind+1:size(bin_act,2);
                cnt=cnt+1;
                bxc(cnt)=bin_act(:,ind)'*bin_act(:,knd)./norm(bin_act(:,ind))./norm(bin_act(:,knd));
            end
        end
        mean_bxc_new(find(hom_data2(region_ind).raw_data(t_ind).timepoint==timepoints),region_ind)=nanmean(bxc);
    end
end

nc=nanmean(nanmean(mean_bxc_new(1:2,:)'));
nc2=nanmean(nanmean(mean_bxc2(1:2,:)'));

figure;hold on
plot(timepoints(1:2),nanmean(mean_bxc_new(1:2,:)')/nc,'ko','linewidth',n)
plot(timepoints(3:6),nanmean(mean_bxc_new(3:6,:)')/nc,'ko','linewidth',n)
errorbar(timepoints(1:2),nanmean(mean_bxc_new(1:2,:)')/nc,(nanstd(mean_bxc_new(1:2,:)')/sqrt(size(mean_bxc_new(1:2,:),2)))/nc, 'k', 'LineWidth', n);
errorbar(timepoints(3:6),nanmean(mean_bxc_new(3:6,:)')/nc,(nanstd(mean_bxc_new(3:6,:)')/sqrt(size(mean_bxc_new(3:6,:),2)))/nc, 'k', 'LineWidth', n);

